<?php
namespace Home\Controller;
class ActiveController extends \Home\Controller\BaseController
{
	private static $prefix = 'active_';
	
	/**
	 * 首页活动图片
	 * @author 刘新
	 */
	public function indexActive()
	{
		//先从获取缓存信息
//		$task = S('index_banners', '');
//		if($task) $this->ajaxReturn(array('error' => 0, 'info' => $task));

		//查询banner活动
		$task = M('Task')->alias('t')->join('__TASK_TYPE__ as tt on t.type_id=tt.id')->where(array('code'=>'banner','tt.status'=>1,'t.status'=>1))->field('t.name,t.id,t.file_ids,t.target_url')->select();
		if(!$task) $this->ajaxReturn(array('error' => 1, 'info' => '首页banner获取失败'));

		$ids = array();
		foreach($task as $key => $val) {
			$val = explode(',', $val['file_ids']);
			$task[$key]['pic_id'] = $val[0];
			$ids[] = $val[0];
		}
		
		//查询banner活动对应的图片
		$where['status'] = 1;
		$where['id'] = array('in', $ids);
		$list = $this->returnList('File', $where, '', 'id,name,url');
		if(!$list) $this->ajaxReturn(array('error' => 1, 'info' => '活动图片获取失败'));
		$tmp = array();
		
		foreach($list as $k => $v) {
			$v['pic_id'] = $v['id'];
			unset($v['id']);
			$tmp[$v['pic_id']] = $v;
		}
		
		$default = global_config('default_picture');
		foreach($task as $key => &$val) {
			if($tmp[$val['pic_id']]) {
				$val = array_merge($val, $tmp[$val['pic_id']]);
			} else {
				//$this->errorLog(__FILE__, __LINE__, "活动\"{$val['name']}\"没有关联的图片或关联的图片不存在");
				$val['pic_id'] = '';
				$val['url'] = $default;
			}
			unset($val['file_ids']);
			unset($val['name']);
		}
		//缓存数据
		S('index_banners', $task, array('expire'=> 24*3600));
		$this->ajaxReturn(array('error' => 0, 'info' => $task));
	}
	
	/**
	 * 活动详情
	 * @author 刘新
	 */
	public function activeDetail()
	{
		//先从缓存中获取数据
		$active = S('active_detail', '', array('prefix'=>self::$prefix));
		if($active) $this->ajaxReturn(array('error' => 0, 'info' => $active));
		
		//要据ID查询
		$param = I('params');
		if(!$param) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		$id = $param['id'];
		
		//查询活动信息
		$active = $this->returnOne('Task', array('id'=>$id));
		if(!$active) $this->ajaxReturn(array('error' => 1, 'info' => '查询失败'));
		$active['file_ids'] = explode(',', $active['file_ids']);
		
		//查询活动类型
		$typeName = M('TaskType')->where(array('status' => 1, 'id' => $active['type_id']))->getField('name');
		$active['type_name'] = $typeName;
		
		//缓存数据
		S('active_detail', $active, array('expire'=>24*3600,'prefix'=>self::$prefix));
		$this->ajaxReturn(array('error' => 0, 'info' => $active));
	}
	/**
	 * 获取详情页面
	 */
	public function getActiveInfo(){
		$param = I('params');
		$id = $param['id'];
		$task = M("Task")->where(array("id"=>$id))->find();
		$this->ajaxReturn(array('error' => 0, 'info' => $task['description']));
	}
}